package com.example.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.demo.common.PageData;
import com.example.demo.common.ResponseData;
import com.example.demo.domain.User.UserInfo;
import com.example.demo.domain.User.UserLogin;
import com.example.demo.domain.User.UserQuery;
import com.example.demo.mapper.userMapper.UserMapper;
import com.example.demo.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService{
    String str1="^(?=.*[0-9])";
    String str2="^(?=.*[a-zA-Z])";
    String str3="^(?=.*[!@#$%^&*()_+\\-=\\[\\];':\"\\\\|,.<>/?]).+$";
 @Autowired
 UserMapper userMapper;
        /**
            * 注册用户
            *校验参数，判断用户是否已经存在
         * **/
    @Override
    public ResponseData<String> registUser(UserInfo userInfo) {

        if (userInfo.getPwd().matches(str1)){
            return ResponseData.fail("至少有一个数字");
        }
        if (userInfo.getPwd().matches(str2)){
            return ResponseData.fail("至少有一个英文");
        }
        if (!userInfo.getPwd().matches(str3)){
            return ResponseData.fail("至少有一个符号");
        }
        if (userInfo.getPwd().length() < 8){
            return ResponseData.fail("密码需要8位数以上");
        }
        int i = userMapper.registUser(userInfo);
        if (i <= 0){
            return ResponseData.fail();
        }

            return ResponseData.ok();
    }

    /**登录用户*/
    @Override
    public ResponseData<String> loginUser(UserLogin userLogin) {
        if (StrUtil.isEmpty(userLogin.getUserName())){
            return ResponseData.fail("用户名不能为空");
        }
        if (StrUtil.isEmpty(userLogin.getPwd())){

            return ResponseData.fail("密码不能为空");
        }
        //判断用户是否存在   查询数据库是否有这个用户
        UserInfo getUser=userMapper.getUser(userLogin.getUserName());
        if (ObjectUtil.isNotNull(getUser)){
            return ResponseData.ok("登录成功");
        }
        //判断密码
        String pwd= SecureUtil.sha256(userLogin.getPwd());
        if (!StrUtil.equals(pwd,getUser.getPwd())){
            return ResponseData.fail("密码错误");
        }
        return ResponseData.ok();
    }


    /**查询接口*/
    @Override
    public ResponseData<UserInfo> getUser(String userName) {
        UserInfo userInfo = userMapper.getUser(userName);
        if (ObjectUtil.isEmpty(userInfo)) {
            return ResponseData.fail("未查询到用户");
        }
        return ResponseData.ok(userInfo);
    }

    /**删除用户*/
    @Override
    public ResponseData<String> deleteUser(String userName) {
        int i = userMapper.deleteUser(userName);
        if (i <= 0){//i是受影响数，删除的记录<=0行的话报错
            return ResponseData.fail("删除失败,至少删除一条");
        }
        return ResponseData.delete();
    }



    @Override
    public ResponseData<PageData<UserInfo>> page(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPageStart(),userQuery.getPageSize());
        PageInfo<UserInfo> pageInfo=new PageInfo<>(userMapper.pageQuery(userQuery));
        PageData<UserInfo> pageData=new PageData<>(pageInfo.getTotal(),pageInfo.getList());
        return ResponseData.ok(pageData);
    }

    /**新增用户*/
    @Override
    public ResponseData<String> addUser(UserInfo userInfo) {
        userInfo.setPwd(SecureUtil.sha256(userInfo.getPwd()));
        int i = userMapper.addUser(userInfo);
        if (i <= 0){
            return ResponseData.fail();
        }
        return ResponseData.ok();
    }

    /**修改用户*/
    @Override
    public ResponseData<Boolean> modifyUser(UserInfo userInfo) {
        int i=userMapper.modifyUser(userInfo);
        if (i<=0){
            return ResponseData.fail();
        }
        return ResponseData.modify();
    }
//    @Override
//    public ResponseData<String> regist(UserInfo userInfo) {
//
//        if (StrUtil.isEmpty(userInfo.getUserName())){
//            return ResponseData.fail("用户名不能为空");
//        }
//        if (StrUtil.isEmpty(userInfo.getPwd())){
//
//            return ResponseData.fail("密码不能为空");
//        }
//        if (userInfo.getPwd().matches(str1)){
//            return ResponseData.fail("至少有一个数字");
//        }
//        if (userInfo.getPwd().matches(str2)){
//            return ResponseData.fail("至少有一个英文");
//        }
//        if (!userInfo.getPwd().matches(str3)){
//            return ResponseData.fail("至少有一个符号");
//        }
//        if (userInfo.getPwd().length() < 8){
//            return ResponseData.fail("密码需要8位数以上");
//        }else{
//            return ResponseData.ok();
//        }
//    }

}
